package com.yxcz.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/Common")
public class CommonController {

	@RequestMapping("/header")
	public String header() {
		return "Common/header";

	}

	@RequestMapping("/footer")
	public String footer() {

		return "Common/footer";

	}

	@RequestMapping("/404Page")
	public String _404Page() {

		return "Common/404";

	}

	@RequestMapping("/500Page")
	public String _500Page() {

		return "Common/500";

	}

	@RequestMapping("/download")
	public String download(String fileName, HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
		try {
			String path = request.getSession().getServletContext().getRealPath("/resources/download");// 这个download目录为啥建立在classes下的
			InputStream inputStream = new FileInputStream(new File(path + File.separator + fileName));

			OutputStream os = response.getOutputStream();
			byte[] b = new byte[2048];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}

			// 这里主要关闭。
			os.close();

			inputStream.close();
		} catch (FileNotFoundException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
		// 返回值要注意，要不然就出现下面这句错误！
		// java+getOutputStream() has already been called for this response
		return null;
	}
}
